//
//  GAlertLoadingView.m
//  iphone51buy
//
//  Created by icson apple on 12-6-13.
//  Copyright (c) 2012年 icson. All rights reserved.
//

#import "GAlertLoadingView.h"

@implementation GAlertLoadingView

static GAlertLoadingView *_sharedAlertLoadingView;
+(GAlertLoadingView *)sharedAlertLoadingView
{
    @synchronized([GAlertLoadingView class]){
		if (!_sharedAlertLoadingView) {
			_sharedAlertLoadingView = [[GAlertLoadingView alloc] init];
		}

		return _sharedAlertLoadingView;
	}

	return nil;
}

- (void)dealloc{
    [indicatorView release];
    [bgView release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
		bgView = [[UIView alloc] initWithFrame: CGRectZero];
		[bgView setBackgroundColor: [UIColor blackColor]];
        bgView.layer.opaque = NO;
		bgView.layer.opacity = 0.3;
		[self addSubview: bgView];
		[self sendSubviewToBack: bgView];

		centerView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 40.0, 40.0)];
		[centerView setBackgroundColor: [UIColor whiteColor]];
		centerView.layer.cornerRadius = 5.0;

		indicatorView = [[GActivityIndicatorView alloc] initWithFrame: CGRectMake(0.0, 0.0, 17.0, 31.0)];
		[indicatorView setCenter: CGPointMake(20.0, 20.0)];
		[centerView addSubview: indicatorView];

		[self addSubview: centerView];
    }
    return self;
}

- (void)show
{
	[self setBackgroundColor: [UIColor clearColor]];
	UIWindow *w = [UIApplication sharedApplication].keyWindow;

	CGSize wSize = w.frame.size;
	self.frame = CGRectMake(0.0, 0.0, wSize.width, wSize.height);
	bgView.frame = CGRectMake(0.0, 0.0, wSize.width, wSize.height);
	[w addSubview: self];
	[w bringSubviewToFront: self];

	[centerView setCenter: CGPointMake(wSize.width / 2.0, wSize.height / 2.0)];
	[indicatorView startAnimating];
}

- (void)hide
{
	[indicatorView stopAnimating];
	[self removeFromSuperview];
}
@end
